package com.liliudong.cursormate.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liliudong.cursormate.entity.SysPermission;
import com.liliudong.cursormate.entity.SysRolePermission;
import com.liliudong.cursormate.mapper.SysRolePermissionMapper;
import com.liliudong.cursormate.service.SysRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author Liudong.li
 * @description 针对表【sys_role_permission(系统角色权限表)】的数据库操作Service实现
 * @createDate 2023-10-24 22:29:24
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission>
        implements SysRolePermissionService {

    @Override
    public void removeByRoleId(Long roleId) {
        this.remove(
                Wrappers.lambdaQuery(SysRolePermission.class)
                        .eq(SysRolePermission::getRoleId, roleId)
        );
    }

    @Override
    @Transactional
    public void saveBatchByRoleId(Long roleId, List<SysPermission> sysPermissionList) {
        if (CollUtil.isNotEmpty(sysPermissionList)) {
            List<SysRolePermission> sysRolePermissionList = sysPermissionList.stream()
                    .map(sysPermission -> {
                        SysRolePermission sysRolePermission = new SysRolePermission();
                        sysRolePermission.setRoleId(roleId);
                        sysRolePermission.setPermissionId(sysPermission.getId());
                        return sysRolePermission;
                    }).collect(Collectors.toList());
            this.saveBatch(sysRolePermissionList);
        }
    }
}




